/**
 *
 */
package com.zhuoyue.zhtj.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhuoyue.zhtj.common.ServerResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.zhtj.entity.Wxxx;
import com.zhuoyue.zhtj.service.WxxxService;


/**
 * 微信信息Controller
 *
 * @author hss
 * @version 2017-08-16
 */
@Controller
@RequestMapping(value = "${adminPath}/zhtj/wxxx")
public class WxxxController extends BaseController {

    @Autowired
    private WxxxService wxxxService;

    @ModelAttribute
    public Wxxx get(@RequestParam(required = false) String id) {
        Wxxx entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = wxxxService.get(id);
        }
        if (entity == null) {
            entity = new Wxxx();
        }
        return entity;
    }

    @RequiresPermissions("zhtj:wxxx:view")
    @RequestMapping(value = {"list", ""})
    public String list(Wxxx wxxx, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<Wxxx> page = wxxxService.findPage(new Page<Wxxx>(request, response), wxxx);
        model.addAttribute("page", page);
        return "zhuoyue/zhtj/wxxxList";
    }

    @RequiresPermissions("zhtj:wxxx:view")
    @RequestMapping(value = "form")
    public String form(Wxxx wxxx, Model model) {
        model.addAttribute("wxxx", wxxx);
        return "zhuoyue/zhtj/wxxxForm";
    }

    @RequiresPermissions("zhtj:wxxx:edit")
    @RequestMapping(value = "save")
    public String save(Wxxx wxxx, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, wxxx)) {
            return form(wxxx, model);
        }
        wxxxService.save(wxxx);
        addMessage(redirectAttributes, "保存微信信息成功");
        return "redirect:" + Global.getAdminPath() + "/zhtj/wxxx/?repage";
    }

    @RequiresPermissions("zhtj:wxxx:edit")
    @RequestMapping(value = "delete")
    public String delete(Wxxx wxxx, RedirectAttributes redirectAttributes) {
        wxxxService.delete(wxxx);
        addMessage(redirectAttributes, "删除微信信息成功");
        return "redirect:" + Global.getAdminPath() + "/zhtj/wxxx/?repage";
    }

    /**
     * 获取用户信息链接地址
     * @param response
     * @return
     */
    @RequestMapping(value = "getWxUserInfoUrl")
    public String getWxUserInfoUrl(HttpServletResponse response) {
        String url = wxxxService.getUrl();
        if (url != null) {
            return this.renderString(response, ServerResponse.createBySuccess("获取链接成功", url));
        }
        return this.renderString(response, ServerResponse.createByErrorMessage("获取链接失败"));
    }

    @RequestMapping(value = "getZbUrl")
    public String getZbUrl(HttpServletResponse response) {
        String url = wxxxService.getZbUrl();
        if (url != null) {
            return this.renderString(response, ServerResponse.createBySuccess("获取链接成功", url));
        }
        return this.renderString(response, ServerResponse.createByErrorMessage("获取链接失败"));
    }

    /**
     * 微信授权回调
     * @param code
     * @param request
     * @param response
     * @param model
     * @return
     */
//    @RequestMapping(value = "grantAuth")
//    public String grantAuth(String code, HttpServletRequest request,HttpServletResponse response, Model model) {
//        Wxxx userInfo = null;
//        if (code != null) {
//            userInfo = wxxxService.getWxUserInfo(code);
//        }
//        if (userInfo != null) {
//            if(!wxxxService.checkOpeId(userInfo.getOpenid())){
//                userInfo.setId(IdGen.uuid());
//                wxxxService.insert(userInfo);
//            }
//			model.addAttribute("wxuser",userInfo);
//
//        }
//
//    }


    @RequestMapping(value = "grantAuth")
    public String grantAuth(String code, HttpServletRequest request,HttpServletResponse response, Model model) {
        Wxxx userInfo = null;
        System.out.println(code);
        if (code != null&&!"".equals(code)) {
            userInfo = wxxxService.getWxUserInfo(code);
        }
        if (userInfo != null) {
            return this.renderString(response,ServerResponse.createBySuccess("此微信已关注微信公众号"));
        }
        return this.renderString(response,ServerResponse.createByErrorMessage("此微信尚未关注微信公众号"));
    }

    @RequestMapping(value = "prompt")
    public String prompt(HttpServletResponse response){
        String qrcode_img_url=wxxxService.prompt();
        if(qrcode_img_url!=null) {
            return this.renderString(response, ServerResponse.createBySuccess("获取二维码成功", qrcode_img_url));
        }
        return this.renderString(response,ServerResponse.createByErrorMessage("获取二维码失败"));
    }
}